<?php
namespace app\admin\controller;

use app\admin\model\Annual;
use app\admin\model\Memb;
use app\admin\model\User;
use app\admin\model\School;
use app\admin\service\SportService;
use app\admin\model\Sport as SportModel;
use app\admin\model\SportDel;
use app\admin\model\TaskscoreRecord;
use app\admin\model\Match;
use app\admin\model\Standard;
use app\admin\model\Project;
use app\admin\model\Member;
use app\admin\model\SportRecord;
use app\admin\controller\Tongbu;
use think\Db;

class Sport extends Backend
{
    protected function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub
        $this->service = new SportService();
        $this->model = new SportModel();
    }

    public function record(){
        $uid = $this->userId;
        $result = $this->service->getList($uid);
        return $this->jsonReturn($result);
    }

    public function rankRecord(){
        $uid = $this->userId;
        $result = $this->service->getRankList($uid);
        return $this->jsonReturn($result);
    }

    public function delrecord(){
        $uid = $this->userId;
        $result = $this->service->getdelList($uid);
        return $this->jsonReturn($result);
    }

    public function delrecover(){
        $uid = $this->userId;
        $result = $this->service->getdelrecoverList($uid);
        return $this->jsonReturn($result);
    }

    /**
     * 恢复
     * @param array $data 数据源
     * @param bool $is_cache 是否设置缓存true或false
     * @return bool 返回结果
     * @throws \Exception
     * @author 牧羊人
     * @date 2019/3/27
     */
    public function recover()
    {
        // 参数
        $param = request()->param();
        // 记录ID
        $ids = getter($param, "id");
        if (empty($ids)) {
            return $this->jsonReturn(message("记录ID不能为空", false));
        }
        $tongbu = new Tongbu();
        $sd = new SportDel();
        if (is_array($ids)) {
            $result = false;
            // $list = SportDel::where([['tbid','in',$ids]])->select();
            foreach ($ids as $val) {
                $reco = SportDel::where('tbid',$val)->find();
                if ($reco['createdDate']) {
                    $createtime = strtotime($reco['createdDate']);
                    $result = $sd->doDelete(['tbid'=>$val]);
                    if ($result) {
                        $ss = $tongbu->setongbuSport(date('Y-m-d H:i:s',$createtime-60),date('Y-m-d H:i:s',$createtime+60));
                    }

                }

            }

            // $result = $sd->deleteDAll($ids,true);
            if (!$result) {
                return $this->jsonReturn(message("恢复失败", false));
            }

            return $this->jsonReturn(message("恢复成功",true));
        }else{
            //从平台恢复数据
            $reco = SportDel::where('tbid',$ids)->find();
            if ($reco['createdDate']) {
                $createtime = strtotime($reco['createdDate']);
                $result = $sd->doDelete(['tbid'=>$ids]);
                if ($result) {
                    $ss = $tongbu->setongbuSport(date('Y-m-d H:i:s',$createtime-60),date('Y-m-d H:i:s',$createtime+60));
                }
                return $this->jsonReturn(message("恢复成功",true,date('Y-m-d H:i:s',$createtime-60)));
            }
        }
        return $this->jsonReturn(message("恢复失败，无运动时间",false));
        // $result = SportDel::doDelete(['tbid'=>$id]);


    }

    /**
     * 导入Excel
     * @author 牧羊人
     * @since 2021/5/24
     */
    public function importExcelscore()
    {
        $error = "";
        // 上传文件(非图片)
        $result = upload_file('file', '', $error);
        if (!$result) {
            return $this->jsonReturn(message($error, false));
        }
        // 文件路径
        $filePath = UPLOAD_TEMP_PATH . $result;
        if (!file_exists($filePath)) {
            return $this->jsonReturn(message("文件不存在", false));
        }
        // 读取文件
        $objPHPExcel = \PHPExcel_IOFactory::load($filePath); //获取sheet表格数目
        $sheetCount = $objPHPExcel->getSheetCount(); //默认选中sheet0表
        $sheetSelected = 0;
        $objPHPExcel->setActiveSheetIndex($sheetSelected); //获取表格行数
        $rowCount = $objPHPExcel->getActiveSheet()->getHighestRow(); //获取表格列数
        $columnCount = $objPHPExcel->getActiveSheet()->getHighestColumn();
        // 计数器
        $totalNum = 0;
        // 循环读取行数据
        $data = [];
        for ($row = 2; $row <= $rowCount; $row++) {
            // 获取列值
            $dataArr = array();
            // 体测
            $matchname = $objPHPExcel->getActiveSheet()->getCell("A" . $row)->getValue();
            // 项目
            $projectname = $objPHPExcel->getActiveSheet()->getCell("B" . $row)->getValue();
            // 姓名
            $name = $objPHPExcel->getActiveSheet()->getCell("C" . $row)->getValue();
            // 学籍号
            $roll = $objPHPExcel->getActiveSheet()->getCell("D" . $row)->getValue();
            // 运动成绩
            $score = $objPHPExcel->getActiveSheet()->getCell("E" . $row)->getValue();
            // 单位
            $unit = $objPHPExcel->getActiveSheet()->getCell("F" . $row)->getValue();
            //性别
            $gender = $objPHPExcel->getActiveSheet()->getCell("G" . $row)->getValue();
            //运动时间
            $sportdate = $objPHPExcel->getActiveSheet()->getCell("H" . $row)->getValue();
            $projectid = Project::where(['name'=>$projectname,'mark'=>1])->value('id');
            $member = Member::where(['realname'=>$name,'roll'=>$roll,'mark'=>1])->find();

            // $rid = TaskscoreRecord::where(['memberId'=>$memberid,'taskId'=>$matchid,'score'=>$score])->value('id');
            $mlist = Member::where(['roll'=>$roll,'mark'=>1])->cache(300)->find();
            $school_name = School::where('id',$mlist['organization'])->value('name');
            if ($mlist) { //判断匹配学籍号
                $vs = [];
                $vs['type'] = 4;
                $vs['memberId'] = $member['id'];
                $vs['schoolId'] = $mlist['organization'];
                $vs['gradeId'] = $mlist['grade'];
                $vs['classId'] = $mlist['class'];
                $vs['departmentsId'] = $mlist['superior'];
                $vs['tiyu_superior'] = $mlist['tiyu_superior'];
                $vs['professionalId'] = $mlist['professional'];
                $vs['tiyu_professional'] = $mlist['tiyu_professional'];
                $vs['tiyu_class'] = $mlist['features']?intval(json_decode($mlist['features'],true)[0]):0;
                $vs['tiyu_class_string'] = $mlist['features'];
                $vs['projectId'] = $projectid;
                $vs['hasTest'] = 1;
                $vs['testerName'] = $mlist['realname'];
                $vs['testerNumber'] = $mlist['roll'];
                $vs['grade'] = $mlist['grade'];
                $vs['clazz'] = $mlist['class'];
                $vs['itemCode'] = Project::where(['name'=>$projectname])->value('code');
                if ($vs['itemCode']=='weight'||$vs['itemCode']=='height') {
                    $vs['projectId'] = Project::where(['organization'=>$vs['schoolId'],'code'=>'BMI','mark'=>1])->cache(115)->value('id');
                }
                $matchid = Match::where(['name'=>$matchname,'projectId'=>$vs['projectId'],'mark'=>1])->value('id');
                $vs['id'] = $vs['tbid'] = '9999'.$member['id'].$matchid.$projectid;
                $stanArr = json_decode(Standard::where(['project'=>$vs['projectId'],'mark'=>1])->value('standard'),true);
                $vs['taskId'] = $matchid;
                $vs['score'] = $score;
                $vs['unit'] = $unit;
                if ($stanArr) {
                    foreach ($stanArr as $vv) {
                        if ($vv['sex']==$mlist['gender']) {
                            switch ($vv['conditions']) {//判断成绩评级
                                case 'gt':
                                    if ($vs['score']>$vv['target'][0]) {
                                        $vs['rating'] = $vv['level'];
                                        $vs['pingscore'] = $vv['score'];
                                    }
                                    break;
                                case 'egt':
                                    if ($vs['score']>=$vv['target'][0]) {
                                        $vs['rating'] = $vv['level'];
                                        $vs['pingscore'] = $vv['score'];
                                    }
                                    break;
                                case 'lt':
                                    if ($vs['score']<$vv['target'][0]) {
                                        $vs['rating'] = $vv['level'];
                                        $vs['pingscore'] = $vv['score'];
                                    }
                                    break;
                                case 'elt':
                                    if ($vs['score']<=$vv['target'][0]) {
                                        $vs['rating'] = $vv['level'];
                                        $vs['pingscore'] = $vv['score'];
                                    }
                                    break;
                                case 'between':
                                    if ($vs['score']>=$vv['target'][0]&&$vs['score']<=$vv['target'][1]) {
                                        $vs['rating'] = $vv['level'];
                                        $vs['pingscore'] = $vv['score'];
                                    }
                                    break;

                                default:
                                    $vs['rating'] = 0;
                                    break;
                            }
                        }
                    }
                }
                //归类统计
                $vs['stype'] = Project::where(['organization'=>$vs['schoolId'],'code'=>$vs['itemCode'],'mark'=>1])->value('type');
                $vs['gender'] = $gender=='男'?1:2;
                if (isset($vs['videoUrlList'])) {
                    $vs['videoUrlList'] = json_encode($vs['videoUrlList']);
                }
                $vs['createdDate'] = date('Y-m-d H:i:s',strtotime($sportdate));
                $data[] = $vs;
            }
        }
        $sportrecord = new SportRecord();
        foreach ($data as $vv) {
            $tbid = $vv['tbid'];
            $id = $sportrecord->where('tbid',$tbid)->cache(150)->value('id');
            if (!$id) {
                $sportrecord->allowField(true)->insert($vv);
            }else{
                $up = $vv;
                $sportrecord->allowField(true)->update($up);
            }
            $totalNum++;
        }
        //计算BMI结果
        // cacl_bmi();
        return $this->jsonReturn(message("本次共更新{$totalNum}条数据", true));
    }

    /**
     * 删除同步记录数据
     * @return mixed
     * @since 2020/11/11
     * @author 牧羊人
     */
    public function deleter()
    {
        $user = new User();
        $uid = $this->userId;
        $userInfo = $user->getInfo($uid);
        if ($userInfo['ish5']!=1&&$userInfo['user_type']==2) {
            return $this->jsonReturn(message("权限不足", false));
        }
        // 参数
        $param = request()->param();
        // 记录ID
        $ids = getter($param, "id");
        if (empty($ids)) {
            return message("记录ID不能为空", false);
        }
        if (is_array($ids)) {
            foreach ($ids as $val) {
                $sportrecord = SportRecord::where('tbid',$val)->find();
                $tbid = Db::name('sportrecord_del')->where(['tbid'=>$val])->value('tbid');
                if (!$tbid) {
                    $data = [
                        'tbid'=>$val,
                        'memberId'=>$sportrecord['memberId'],
                        'schoolId'=>$sportrecord['schoolId'],
                        'gradeId'=>$sportrecord['gradeId'],
                        'classId'=>$sportrecord['classId'],
                        'departmentsId'=>$sportrecord['departmentsId'],
                        'tiyu_superior'=>$sportrecord['tiyu_superior'],
                        'professionalId'=>$sportrecord['professionalId'],
                        'tiyu_professional'=>$sportrecord['tiyu_professional'],
                        'projectId'=>$sportrecord['projectId'],
                        'tiyu_class'=>$sportrecord['tiyu_class'],
                        'tiyu_class_string'=>$sportrecord['tiyu_class_string'],
                        'taskId'=>$sportrecord['taskId'],
                        'testerName'=>$sportrecord['testerName'],
                        'testerNumber'=>$sportrecord['testerNumber'],
                        'idCard'=>$sportrecord['idCard'],
                        'gender'=>$sportrecord['gender'],
                        'school'=>$sportrecord['school'],
                        'grade'=>$sportrecord['grade'],
                        'clazz'=>$sportrecord['clazz'],
                        'itemCode'=>$sportrecord['itemCode'],
                        'score'=>$sportrecord['score'],
                        'unit'=>$sportrecord['unit'],
                        'duration'=>$sportrecord['duration'],
                        'pingscore'=>$sportrecord['pingscore'],
                        'rating'=>$sportrecord['rating'],
                        'bmi'=>$sportrecord['bmi'],
                        'createdDate'=>$sportrecord['createdDate'],
                        'deletetime'=>date('Y-m-d H:i:s'),
                        'deleteuser'=>$uid
                    ];
                    Db::name('sportrecord_del')->insert($data);
                }

            }
            // 批量删除
            $result = $this->model->deleteDAll($ids,true);
            if (!$result) {
                return message("删除失败", false);
            }
            return message("删除成功");
        } else {
            // 单个删除
            $info = $this->model->getInfo($ids);
            if ($info) {
                $sportrecord = SportRecord::where('tbid',$ids)->find();
                $tbid = Db::name('sportrecord_del')->where(['tbid'=>$ids])->value('tbid');
                if (!$tbid) {
                    $data = [
                        'tbid'=>$val,
                        'memberId'=>$sportrecord['memberId'],
                        'schoolId'=>$sportrecord['schoolId'],
                        'gradeId'=>$sportrecord['gradeId'],
                        'classId'=>$sportrecord['classId'],
                        'departmentsId'=>$sportrecord['departmentsId'],
                        'tiyu_superior'=>$sportrecord['tiyu_superior'],
                        'professionalId'=>$sportrecord['professionalId'],
                        'tiyu_professional'=>$sportrecord['tiyu_professional'],
                        'projectId'=>$sportrecord['projectId'],
                        'tiyu_class'=>$sportrecord['tiyu_class'],
                        'tiyu_class_string'=>$sportrecord['tiyu_class_string'],
                        'taskId'=>$sportrecord['taskId'],
                        'testerName'=>$sportrecord['testerName'],
                        'testerNumber'=>$sportrecord['testerNumber'],
                        'idCard'=>$sportrecord['idCard'],
                        'gender'=>$sportrecord['gender'],
                        'school'=>$sportrecord['school'],
                        'grade'=>$sportrecord['grade'],
                        'clazz'=>$sportrecord['clazz'],
                        'itemCode'=>$sportrecord['itemCode'],
                        'score'=>$sportrecord['score'],
                        'unit'=>$sportrecord['unit'],
                        'duration'=>$sportrecord['duration'],
                        'pingscore'=>$sportrecord['pingscore'],
                        'rating'=>$sportrecord['rating'],
                        'bmi'=>$sportrecord['bmi'],
                        'createdDate'=>$sportrecord['createdDate'],
                        'deletetime'=>date('Y-m-d H:i:s'),
                        'deleteuser'=>$uid
                    ];
                    Db::name('sportrecord_del')->insert($data);
                }
                $result = $this->model->doDelete(['id'=>$ids]);
                if (!$result) {
                    return $this->jsonReturn(message("删除失败", false));
                }
                return $this->jsonReturn(message());

            }
            return message($this->model->getError(), false);
        }

    }

    /**
     * 导入Excel 批量删除
     * @author 牧羊人
     * @since 2021/5/24
     */
    public function importExceldel()
    {
        $uid = $this->userId;
        $error = "";
        // 上传文件(非图片)
        $result = upload_file('file', '', $error);
        if (!$result) {
            return $this->jsonReturn(message($error, false));
        }
        // 文件路径
        $filePath = UPLOAD_TEMP_PATH . $result;
        if (!file_exists($filePath)) {
            return $this->jsonReturn(message("文件不存在", false));
        }
        // 读取文件
        $objPHPExcel = \PHPExcel_IOFactory::load($filePath); //获取sheet表格数目
        $sheetCount = $objPHPExcel->getSheetCount(); //默认选中sheet0表
        $sheetSelected = 0;
        $objPHPExcel->setActiveSheetIndex($sheetSelected); //获取表格行数
        $rowCount = $objPHPExcel->getActiveSheet()->getHighestRow(); //获取表格列数
        $columnCount = $objPHPExcel->getActiveSheet()->getHighestColumn();
        // 计数器
        $totalNum = 0;
        $totaltNum = 0;
        // 循环读取行数据
        $data = [];
        for ($row = 2; $row <= $rowCount; $row++) {
            // 获取列值
            $dataArr = array();
            // 体测
            $matchname = $objPHPExcel->getActiveSheet()->getCell("A" . $row)->getValue();
            // 项目
            $projectname = $objPHPExcel->getActiveSheet()->getCell("B" . $row)->getValue();
            // 姓名
            $name = $objPHPExcel->getActiveSheet()->getCell("C" . $row)->getValue();
            // 学籍号
            $roll = $objPHPExcel->getActiveSheet()->getCell("D" . $row)->getValue();
            // 运动成绩
            // $score = $objPHPExcel->getActiveSheet()->getCell("E" . $row)->getValue();
            //性别
            $gender = $objPHPExcel->getActiveSheet()->getCell("E" . $row)->getValue();
            $projectid = 0;
            if($projectname){
                $projectid = Project::where(['name'=>$projectname,'mark'=>1])->value('id');
            }
            $memberid = Member::where(['realname'=>$name,'roll'=>$roll,'mark'=>1])->value('id');
            $matchwhere = [];
            $matchwhere[] = ['name','=',$matchname];
            $matchwhere[] = ['mark','=',1];
            if ($projectid) {
                $matchwhere[] = ['projectId','=',$projectid];
            }

            $matchid = Match::where($matchwhere)->column('id');

            $wheredel = [];
            if ($memberid) {
                $wheredel[] = ['memberId','=',$memberid];
            }
            if ($projectid) {
                $wheredel[] = ['projectId','=',$projectid];
            }
            if ($matchid) {
                $wheredel[] = ['taskId','in',$matchid];
            }
            // if ($score) {
            //     $wheredel['score'] = $score;
            // }
            // halt($wheredel);
            $ids = SportRecord::where($wheredel)->column('id');

            foreach ($ids as $val) {
                $sportrecord = SportRecord::where('tbid',$val)->find();
                $tbid = Db::name('sportrecord_del')->where(['tbid'=>$val])->value('tbid');
                if (!$tbid) {
                    $data = [
                        'tbid'=>$val,
                        'memberId'=>$sportrecord['memberId'],
                        'schoolId'=>$sportrecord['schoolId'],
                        'gradeId'=>$sportrecord['gradeId'],
                        'classId'=>$sportrecord['classId'],
                        'departmentsId'=>$sportrecord['departmentsId'],
                        'tiyu_superior'=>$sportrecord['tiyu_superior'],
                        'professionalId'=>$sportrecord['professionalId'],
                        'tiyu_professional'=>$sportrecord['tiyu_professional'],
                        'projectId'=>$sportrecord['projectId'],
                        'tiyu_class'=>$sportrecord['tiyu_class'],
                        'tiyu_class_string'=>$sportrecord['tiyu_class_string'],
                        'taskId'=>$sportrecord['taskId'],
                        'testerName'=>$sportrecord['testerName'],
                        'testerNumber'=>$sportrecord['testerNumber'],
                        'idCard'=>$sportrecord['idCard'],
                        'gender'=>$sportrecord['gender'],
                        'school'=>$sportrecord['school'],
                        'grade'=>$sportrecord['grade'],
                        'clazz'=>$sportrecord['clazz'],
                        'itemCode'=>$sportrecord['itemCode'],
                        'score'=>$sportrecord['score'],
                        'unit'=>$sportrecord['unit'],
                        'duration'=>$sportrecord['duration'],
                        'pingscore'=>$sportrecord['pingscore'],
                        'rating'=>$sportrecord['rating'],
                        'bmi'=>$sportrecord['bmi'],
                        'createdDate'=>$sportrecord['createdDate'],
                        'deletetime'=>date('Y-m-d H:i:s'),
                        'deleteuser'=>$uid
                    ];
                    if ($memberid&&$matchid) {
                        $result = $this->model->deleteDAll($val,true);
                        if ($result) {
                            Db::name('sportrecord_del')->insert($data);
                            $totalNum++;
                        }
                    }else{
                        $totaltNum++;
                    }

                }
            }
            // 批量删除


        }

        return $this->jsonReturn(message("本次共删除{$totalNum}条数据,{$totaltNum}条删除失败", true));
    }


    public function exportExcel()
    {
        $param = request()->param();
        $map   = [];

        $user = User::get($this->userId);
        if ($user && $user['user_type'] == '2') {
            $memb  = Memb::where('roll', $user['username'])->cache(500)->find();
            $map[] = ['memberId', '=', $memb['id'] ?? 0];
        } elseif ($user && $user['id'] != 1 && $user['user_type'] == '1' && $user['departmentsId']) {
            $map[] = ['departmentsId', '=', $user['departmentsId']];
        }

        if ($param) {
            // 筛选名称
            if (isset($param['username']) && $param['username']) {
                $map[] = ['a.testerName', 'like', "%{$param['username']}%"];
            }
            // 筛选标题
            if (isset($param['title']) && $param['title']) {
                $map[] = ['a.title', 'like', "%{$param['title']}%"];
            }
            // 项目
            if (isset($param['projectId']) && $param['projectId']) {
                if($param['projectId'] == 39){
                    $map[] = ['a.itemCode', 'in', ['weight','height']];
                }else{
                    $map[] = ['a.projectId', '=', $param['projectId']];
                }
            }
            if (!empty($param['projectCodes'])) {
                if (in_array('BMI', $param['projectCodes'])) {
                    $param['projectCodes'][] = 'weight';
                    $param['projectCodes'][] = 'height';
                }
                $projectIds = (new Project)->whereIn('code', $param['projectCodes'])->column('id');
                $map[] = ['projectId', 'in', $projectIds];
            }
            if (isset($param['type']) && $param['type']) {
                $map[] = ['a.type', '=', $param['type']];
            }
            // 学年
            if (isset($param['annual']) && $param['annual']) {
                $annualResult = (new Annual)->getInfo($param['annual']);
                $starttime    = isset($annualResult['xue_start']) ? trim($annualResult['xue_start']) : '';
                $endtime      = isset($annualResult['xue_end']) ? trim($annualResult['xue_end']) : '';
                if ($starttime && $endtime) {
                    $map[] = ['createdDate', ['>= time', $starttime], ['<= time', $endtime]];
                }
            }
            //年级
            if (isset($param['gradeId']) && $param['gradeId']) {
                $map[] = ['a.gradeId', '=', $param['gradeId']];
            }
            // 院系
            if (isset($param['departmentsId']) && $param['departmentsId']) {
                $map[] = ['a.departmentsId', '=', $param['departmentsId']];
            }
            // 专业
            if (isset($param['professionalId']) && $param['professionalId']) {
                $map[] = ['a.professionalId', '=', $param['professionalId']];
            }
            // 班级
            if (isset($param['classId']) && $param['classId']) {
                $map[] = ['a.classId', '=', $param['classId']];
            }
            // 性别
            if (isset($param['gender']) && $param['gender']) {
                $map[] = ['a.gender', '=', $param['gender']];
            }
            $keywords = getParamStr('keywords') ? (string)$param['keywords'] : '';
            if ($keywords) {
                $map[] = ['testerName|testerNumber', 'like', "%{$keywords}%"];
            } // 时间
            $starttime = isset($param['startTime']) ? trim($param['startTime']) : '';
            $endtime   = isset($param['endTime']) ? trim($param['endTime'] . ' 23:59:59') : '';
            if ($starttime && $endtime) {
                $map[] = ['createdDate', '>= time', $starttime];
                $map[] = ['createdDate', '<= time', $endtime];
            }
        }
        $page     = 1;
        $pageSize = 10000;

        $professionalField = (int)input('departmentsId') == 21 ? 'tiyu_professional' : 'professionalId';
        $classField        = (int)input('departmentsId') == 21 ? 'tiyu_class' : 'classId';
        $standardMap       = ['肥胖', '偏瘦', '正常', '超重'];
        do {
            $list = (new SportModel())
                ->alias('a')
                ->leftJoin(['think_project' => 'b'], 'a.projectId = b.id')
                ->leftJoin(['think_grade' => 'c'], 'a.gradeId = c.id')
                ->leftJoin(['think_clss' => 'd'], 'a.' . $classField . ' = d.id')
                ->leftJoin(['think_professional' => 'e'], 'a.' . $professionalField . ' = e.id')
                ->leftJoin(['think_match' => 'f'], 'a.taskId = f.id')
                ->field(
                    'f.name as task_name, b.name as project_name, testerName as tester_name, testerNumber as tester_number, score, unit, duration, bmi, pingscore, rating, gender, c.name as grade, school, e.name as professional_name, d.name as clazz, a.createdDate as created_date, b.code, a.tiyu_class, a.taskId as task_id, a.memberId as member_id'
                )
                ->page($page++, $pageSize)
                ->where($map)
                ->select();
            $bmiWhere = [];
            foreach ($list as $val) {
                if ($val['code']=='BMI') {
                    $bmiWhere[] = "('{$val['tiyu_class']}', '{$val['task_id']}', '{$val['bmi']}', '{$val['member_id']}')";
                }
            }
            $bmiData = [];
            if ($bmiWhere) {
                $bmiDataTemp = TaskscoreRecord::whereRaw('(tiyu_class,taskId,bmi,memberId) in (' . implode(',', $bmiWhere) . ')')
                    ->where(['itemCode' => ['weight', 'height']])
                    ->field('tiyu_class,taskId,bmi,memberId,itemCode,MAX(score) as score')
                    ->group('tiyu_class,taskId,bmi,memberId,itemCode')
                    ->select()
                    ->toArray();
                foreach ($bmiDataTemp as $val) {
                    $bmiData[$val['tiyu_class']][$val['taskId']][$val['bmi']][$val['memberId']][$val['itemCode']] = $val['score'];
                }
            }
            foreach ($list as $val) {
                if($val['unit'] == 's'){
                    $val['duration'] =  $val['duration'] > 60 ? floor($val['duration']/60)."'".( $val['duration']%60)."'" : $val['duration']."'";
                }
                $val['score'] = $val['score'] . $val['unit'];
                if ($val['code']=='BMI') {
                    if ($val['bmi']) {
                        $val['rating_str'] = $standardMap[$val['rating']] ?? '无效成绩';
                    } else {
                        $val['pingscore'] = '--';
                        $val['rating_str'] = '无效成绩';
                    }
                } else {
                    $val['rating_str'] = ratingStr($val['rating']);
                }
                if ($val['code']=='BMI') {
                    $weight = $bmiData[$val['tiyu_class']][$val['task_id']][$val['bmi']][$val['member_id']]['weight'] ?? 0;
                    $height = $bmiData[$val['tiyu_class']][$val['task_id']][$val['bmi']][$val['member_id']]['height'] ?? 0;
                    if (!$weight || !$height) {
                        $val['score'] = '--';
                    } else {
                        $val['score'] = '体重：'.$weight.'kg　身高：'.$height.'cm';
                    }
                }
                $val['gender'] = $val['gender'] == 1 ? '男' : '女';

                $data[] = $val;
            }
        } while (count($list) == $pageSize);
        $type     = $param['type'] == 4 ? '院系体测' : ($param['type'] == 2 ? '体育锻炼' : '体育赛事');
        $filename = $type . '_运动记录_' . date('YmdHis', time());
        $cell     = [
            'task_name'         => '任务',
            'project_name'      => '项目',
            'tester_name'       => '姓名',
            'tester_number'     => '学籍号',
            'score'             => '运动成绩',
            'duration'          => '运动耗时',
            'bmi'               => 'bmi',
            'pingscore'         => '评分',
            'rating_str'        => '评级',
            'gender'            => '性别',
            'grade'             => '年级',
            'school'            => '院系名称',
            'professional_name' => '专业',
            'clazz'             => '专业班级',
            'created_date'      => '运动时间',
        ];
        downloadCsv($filename, $cell, $data);

        return;
        $list = $listTmp;
        // 实例化PHPExcel类
        $objPHPExcel = new \PHPExcel();
        // 激活当前的sheet表
        $objPHPExcel->setActiveSheetIndex(0);
        //5.设置表格头（即excel表格的第一行）
        $objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', '任务')
            ->setCellValue('B1', '项目')
            ->setCellValue('C1', '姓名')
            ->setCellValue('D1', '学籍号')
            ->setCellValue('E1', '运动成绩')
            ->setCellValue('F1', '运动耗时')
            ->setCellValue('G1', 'bmi')
            ->setCellValue('H1', '评分')
            ->setCellValue('I1', '评级')
            ->setCellValue('J1', '性别')
            ->setCellValue('K1', '年级')
            ->setCellValue('L1', '院系名称')
            ->setCellValue('M1', '专业')
            ->setCellValue('N1', '专业班级')
            ->setCellValue('O1', '运动时间');
        // 设置表格头水平居中
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('A1')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('B1')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('C1')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('D1')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('E1')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('F1')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('G1')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('H1')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('I1')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('J1')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('K1')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('L1')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('M1')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('N1')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('O1')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        //设置列水平居中
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('A')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('B')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('C')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('D')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('E')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('F')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('G')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('H')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('I')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('J')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('K')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('L')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('M')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('N')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('O')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        // 设置单元格宽度
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('A')->setWidth(10);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('B')->setWidth(10);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('C')->setWidth(10);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('D')->setWidth(10);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('E')->setWidth(10);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('F')->setWidth(10);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('G')->setWidth(10);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('H')->setWidth(10);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('I')->setWidth(10);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('J')->setWidth(10);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('K')->setWidth(10);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('L')->setWidth(10);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('M')->setWidth(10);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('N')->setWidth(10);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('O')->setWidth(10);
        // 循环刚取出来的数组，将数据逐一添加到excel表格。
        for ($i = 0; $i < count($list); $i++) {
            $objPHPExcel->getActiveSheet()->setCellValue(
                'A' . ($i + 2),
                Match::where('id', $list[$i]['taskId'])->value('name')
            );                                                                                                   //
            $objPHPExcel->getActiveSheet()->setCellValue('B' . ($i + 2), $list[$i]['projectName']);              //
            $objPHPExcel->getActiveSheet()->setCellValue('C' . ($i + 2), $list[$i]['testerName']);               //
            $objPHPExcel->getActiveSheet()->setCellValue('D' . ($i + 2), $list[$i]['testerNumber']);             //
            $objPHPExcel->getActiveSheet()->setCellValue('E' . ($i + 2), $list[$i]['score']);                    //
            $objPHPExcel->getActiveSheet()->setCellValue('F' . ($i + 2), $list[$i]['duration']);                 //
            $objPHPExcel->getActiveSheet()->setCellValue('G' . ($i + 2), $list[$i]['bmi']);                      //
            $objPHPExcel->getActiveSheet()->setCellValue('H' . ($i + 2), $list[$i]['pingscore']);                //
            $objPHPExcel->getActiveSheet()->setCellValue('I' . ($i + 2), $list[$i]['ratingStr']);                //
            $objPHPExcel->getActiveSheet()->setCellValue('J' . ($i + 2), $list[$i]['gender'] == 1 ? "男" : "女");//
            $objPHPExcel->getActiveSheet()->setCellValue('K' . ($i + 2), $list[$i]['grade']);                    //
            $objPHPExcel->getActiveSheet()->setCellValue('L' . ($i + 2), $list[$i]['school']);                   //
            $objPHPExcel->getActiveSheet()->setCellValue('M' . ($i + 2), $list[$i]['professionalName']);         //
            $objPHPExcel->getActiveSheet()->setCellValue('N' . ($i + 2), $list[$i]['clazz']);                    //
            $objPHPExcel->getActiveSheet()->setCellValue('O' . ($i + 2), $list[$i]['createdDate']);              //
        }
        // 设置保存的Excel表格名称
        $type     = $param['type'] == 4 ? '院系体测' : ($param['type'] == 2 ? '体育锻炼' : '体育赛事');
        $filename = $type . '_运动记录_' . date('YmdHis', time()) . ".xlsx";
        // 设置当前激活的sheet表格名称
        $objPHPExcel->getActiveSheet()->setTitle($type . '记录');

        // 保存本地文件
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
        $objWriter->save(UPLOAD_TEMP_PATH . "/" . $filename);

        return download(UPLOAD_TEMP_PATH . "/" . $filename, $filename);
        // 文件地址
//        $filePath = get_image_url('/uploads'.str_replace(ATTACHMENT_PATH, "", UPLOAD_TEMP_PATH . "/" . $filename));
//        return $this->jsonReturn(message("操作成功", true, $filePath));
    }

    public function exportExcelrecover()
    {
        // 参数
        $param = request()->param();
        // 查询条件
        $map = [];

        if ($param) {
            // 筛选名称
            if (isset($param['username']) && $param['username']) {
                $map[] = ['testerName', 'like', "%{$param['username']}%"];
            }

            // 筛选标题
            if (isset($param['title']) && $param['title']) {
                $map[] = ['title', 'like', "%{$param['title']}%"];
            }

            // 项目
            if (isset($param['projectId']) && $param['projectId']) {
                $map[] = ['projectId', '=', $param['projectId']];
            }

            // if (isset($param['type']) && $param['type']) {
            //     $map[] = ['type', '=', $param['type']];
            // }

            // 学年
            if (isset($param['gradeId']) && $param['gradeId']) {
                $map[] = ['gradeId', '=', $param['gradeId']];
            }

            // 院系
            if (isset($param['departmentsId']) && $param['departmentsId']) {
                $map[] = ['departmentsId', '=', $param['departmentsId']];
            }

            // 专业
            if (isset($param['professionalId']) && $param['professionalId']) {
                $map[] = ['professionalId', '=', $param['professionalId']];
            }

            // 班级
            if (isset($param['classId']) && $param['classId']) {
                $map[] = ['classId', '=', $param['classId']];
            }

            // 性别
            if (isset($param['gender']) && $param['gender']) {
                $map[] = ['gender', '=', $param['gender']];
            }


            // 日期区间查询
            $startEndTime = isset($param['start_end']) ? $param['start_end'] : [];
            if ($startEndTime && is_array($startEndTime)) {
                // 时间区间查询
                $map[] = ['createdDate', 'between time', [$startEndTime[0], $startEndTime[1].' 23:59:59']];
            }

            // 日期区间查询
            $startEndTime2 = isset($param['start_end2']) ? $param['start_end2'] : [];
            if ($startEndTime2 && is_array($startEndTime2)) {
                // 时间区间查询
                $map[] = ['deletetime', 'between time', [$startEndTime2[0], $startEndTime2[1].' 23:59:59']];
            }
        }
        $sportdel = new SportDel();
        // 获取导出的数据源
        $list = $sportdel->where($map)->column('tbid');


        $listTmp = [];
        if (is_array($list)) {
            foreach ($list as $val) {
                $info = $sportdel->getInfo($val);
                $listTmp[] = $info;
            }
        }

        $list = $listTmp;
        // 实例化PHPExcel类
        $objPHPExcel = new \PHPExcel();
        // 激活当前的sheet表
        $objPHPExcel->setActiveSheetIndex(0);
        //5.设置表格头（即excel表格的第一行）
        $objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', '任务')
            ->setCellValue('B1', '项目')
            ->setCellValue('C1', '姓名')
            ->setCellValue('D1', '学籍号')
            ->setCellValue('E1', '运动成绩')
            ->setCellValue('F1', '运动耗时')
            ->setCellValue('G1', 'bmi')
            ->setCellValue('H1', '评分')
            ->setCellValue('I1', '评级')
            ->setCellValue('J1', '性别')
            ->setCellValue('K1', '年级')
            ->setCellValue('L1', '院系名称')
            ->setCellValue('M1', '专业')
            ->setCellValue('N1', '专业班级')
            ->setCellValue('O1', '运动时间');
        // 设置表格头水平居中
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('A1')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('B1')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('C1')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('D1')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('E1')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('F1')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('G1')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('H1')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('I1')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('J1')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('K1')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('L1')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('M1')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('N1')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('O1')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        //设置列水平居中
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('A')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('B')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('C')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('D')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('E')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('F')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('G')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('H')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('I')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('J')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('K')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('L')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('M')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('N')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('O')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        // 设置单元格宽度
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('A')->setWidth(10);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('B')->setWidth(10);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('C')->setWidth(10);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('D')->setWidth(10);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('E')->setWidth(10);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('F')->setWidth(10);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('G')->setWidth(10);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('H')->setWidth(10);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('I')->setWidth(10);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('J')->setWidth(10);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('K')->setWidth(10);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('L')->setWidth(10);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('M')->setWidth(10);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('N')->setWidth(10);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('O')->setWidth(10);
        // 循环刚取出来的数组，将数据逐一添加到excel表格。
        for ($i = 0; $i < count($list); $i++) {
            $objPHPExcel->getActiveSheet()->setCellValue('A' . ($i + 2), Match::where('id',$list[$i]['taskId'])->value('name'));// 
            $objPHPExcel->getActiveSheet()->setCellValue('B' . ($i + 2), $list[$i]['projectName']);// 
            $objPHPExcel->getActiveSheet()->setCellValue('C' . ($i + 2), $list[$i]['testerName']);// 
            $objPHPExcel->getActiveSheet()->setCellValue('D' . ($i + 2), $list[$i]['testerNumber']);// 
            $objPHPExcel->getActiveSheet()->setCellValue('E' . ($i + 2), $list[$i]['score']);// 
            $objPHPExcel->getActiveSheet()->setCellValue('F' . ($i + 2), $list[$i]['duration']);// 
            $objPHPExcel->getActiveSheet()->setCellValue('G' . ($i + 2), $list[$i]['bmi']);// 
            $objPHPExcel->getActiveSheet()->setCellValue('H' . ($i + 2), $list[$i]['pingscore']);// 
            $objPHPExcel->getActiveSheet()->setCellValue('I' . ($i + 2), $list[$i]['ratingStr']);// 
            $objPHPExcel->getActiveSheet()->setCellValue('J' . ($i + 2), $list[$i]['gender']);// 
            $objPHPExcel->getActiveSheet()->setCellValue('K' . ($i + 2), $list[$i]['grade']);// 
            $objPHPExcel->getActiveSheet()->setCellValue('L' . ($i + 2), $list[$i]['school']);// 
            $objPHPExcel->getActiveSheet()->setCellValue('M' . ($i + 2), $list[$i]['professionalName']);// 
            $objPHPExcel->getActiveSheet()->setCellValue('N' . ($i + 2), $list[$i]['clazz']);// 
            $objPHPExcel->getActiveSheet()->setCellValue('O' . ($i + 2), $list[$i]['createdDate']);// 
        }
        // 设置保存的Excel表格名称
        $type = $param['type']==4?'院系体测':($param['type']==2?'体育锻炼':'体育赛事');
        $filename = $type.'_删除记录_' . date('YmdHis', time()) . ".xlsx";
        // 设置当前激活的sheet表格名称
        $objPHPExcel->getActiveSheet()->setTitle($type.'记录');

        // 保存本地文件
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
        $objWriter->save(UPLOAD_TEMP_PATH . "/" . $filename);
        return download(UPLOAD_TEMP_PATH . "/" . $filename, $filename);
        // 文件地址
//        $filePath = get_image_url('/uploads'.str_replace(ATTACHMENT_PATH, "", UPLOAD_TEMP_PATH . "/" . $filename));
//        return $this->jsonReturn(message("操作成功", true, $filePath));
    }

    // 体测数据成绩导出
    public function rankexportExcel()
    {
        $match = new Match();
        // 参数
        $param = request()->param();
        // 查询条件
        $map = [];

        $projectid = 0;
        if ($param) {
            $taskId = isset($param['taskId']) ? (int)$param['taskId'] : 0;

            $matchdata = [];
            if ($taskId) {
                $map[] = ['taskId', '=', $taskId];
                $map[] = ['type', '=', 4];
                $matchdata = $match->where('id',$taskId)->find();
                $projectid = $matchdata['projectId'];
            }
            if ($matchdata&&$matchdata['gradeId']) {
                $map[] = ['gradeId', '=', $matchdata['gradeId']];
            }
            if ($matchdata&&$matchdata['departmentsId']) {
                $map[] = ['departmentsId', '=', $matchdata['departmentsId']];
            }
            // 学年
            if (isset($param['gradeId']) && $param['gradeId']) {
                $map[] = ['gradeId', '=', $param['gradeId']];
            }

            // 院系
            if (isset($param['departmentsId']) && $param['departmentsId']) {
                $map[] = ['departmentsId', '=', $param['departmentsId']];
            }

            // 专业
            if (isset($param['professionalId']) && $param['professionalId']) {
                $map[] = ['professionalId', '=', $param['professionalId']];
            }

            // 班级
            if (isset($param['classId']) && $param['classId']) {
                $map[] = ['classId', '=', $param['classId']];
            }

            // 性别
            if (isset($param['gender']) && $param['gender']) {
                $map[] = ['gender', '=', $param['gender']];
            }

        }
        $f = 'id,memberId,MAX(score) as ascore';
        $sort = 'score desc';
       /* if($projectid == 25){

        }*/
        if(in_array($projectid,[29,31,32])){
            $sort = 'score asc';
            $f = 'id,memberId,min(score) as ascore';
        }
        if($projectid == 39){
            $sort = 'pingscore desc';
            $f = 'id,memberId,MAX(pingscore) as ascore';
        }
        // 获取导出的数据源
        // 获取导出的数据源
        $list2 = $this->model->where($map)->field($f)->group('memberId')
            //->where("testerName='纪佳璐'")
            //->limit(10)
            ->order($sort)
            ->select();
        //->column("id");
        //echo $this->model->getLastSql();die();
        /* $list = [];
         foreach ($list2 as $k=>$v){
             $list[$k]=$v['id'];
         }*/
        //print_r($list);die();
        $listTmp = [];
        $bmiWhere = [];
        foreach ($list2 as $val) {
            $info = $this->model->getInfo($val['id']);
            $info['ascore'] = $val['ascore'];
            if($projectid == 39){
                $info['pingscore'] = $val['ascore'];

                $bmiWhere[] = "('{$info['tiyu_class']}', '{$info['taskId']}', '{$info['bmi']}', '{$info['memberId']}')";
            }
            $listTmp[] = $info;
        }
        $bmiData = [];
        if ($bmiWhere) {
            $bmiDataTemp = TaskscoreRecord::whereRaw('(tiyu_class,taskId,bmi,memberId) in (' . implode(',', $bmiWhere) . ')')
                ->where(['itemCode' => ['weight', 'height']])
                ->field('tiyu_class,taskId,bmi,memberId,itemCode,MAX(score) as score')
                ->group('tiyu_class,taskId,bmi,memberId,itemCode')
                ->select()
                ->toArray();
            foreach ($bmiDataTemp as $val) {
                $bmiData[$val['tiyu_class']][$val['taskId']][$val['bmi']][$val['memberId']][$val['itemCode']] = $val['score'];
            }
        }

        $list = $listTmp;
        // 实例化PHPExcel类
        $objPHPExcel = new \PHPExcel();
        // 激活当前的sheet表
        $objPHPExcel->setActiveSheetIndex(0);
        //5.设置表格头（即excel表格的第一行）
        $objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', '姓名')
            ->setCellValue('B1', '学籍号')
            ->setCellValue('C1', '运动成绩')
            ->setCellValue('D1', 'BMI')
            ->setCellValue('E1', '运动耗时')
            ->setCellValue('F1', '评分')
            ->setCellValue('G1', '性别')
            ->setCellValue('H1', '年级')
            ->setCellValue('I1', '院系名称')
            ->setCellValue('J1', '专业')
            ->setCellValue('K1', '专业班级')
            ->setCellValue('L1', '运动时间');
        // 设置表格头水平居中
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('A1')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('B1')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('C1')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('D1')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('E1')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('F1')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('G1')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('H1')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('I1')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('J1')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('K1')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('L1')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        //设置列水平居中
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('A')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('B')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('C')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('D')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('E')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('F')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('G')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('H')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('I')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('J')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('K')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('L')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        // 设置单元格宽度
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('A')->setWidth(10);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('B')->setWidth(10);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('C')->setWidth(10);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('D')->setWidth(10);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('E')->setWidth(10);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('F')->setWidth(10);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('G')->setWidth(10);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('H')->setWidth(10);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('I')->setWidth(10);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('J')->setWidth(10);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('K')->setWidth(10);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('L')->setWidth(10);
        $xiangmu = '';
        // 循环刚取出来的数组，将数据逐一添加到excel表格。
        for ($i = 0; $i < count($list); $i++) {
            $xiangmu = $list[$i]['projectName'];
            if ($list[$i]['itemCode']=='weight' || $list[$i]['itemCode']=='height') {
                $weight = $bmiData[$list[$i]['tiyu_class']][$list[$i]['taskId']][$list[$i]['bmi']][$list[$i]['memberId']]['weight'] ?? 0;
                $height = $bmiData[$list[$i]['tiyu_class']][$list[$i]['taskId']][$list[$i]['bmi']][$list[$i]['memberId']]['height'] ?? 0;
                $list[$i]['ascore'] = '体重：'.$weight.'kg　身高：'.$height.'cm';
            }
            $objPHPExcel->getActiveSheet()->setCellValue('A' . ($i + 2), $list[$i]['testerName']);// 
            $objPHPExcel->getActiveSheet()->setCellValue('B' . ($i + 2), $list[$i]['testerNumber']);// 
            $objPHPExcel->getActiveSheet()->setCellValue('C' . ($i + 2), $list[$i]['ascore']);//
            $objPHPExcel->getActiveSheet()->setCellValue('D' . ($i + 2), $list[$i]['bmi']);// 
            $objPHPExcel->getActiveSheet()->setCellValue('E' . ($i + 2), $list[$i]['duration']);// 
            $objPHPExcel->getActiveSheet()->setCellValue('F' . ($i + 2), $list[$i]['pingscore']);// 
            $objPHPExcel->getActiveSheet()->setCellValue('G' . ($i + 2), $list[$i]['gender']==1 ? "男" : "女");// 
            $objPHPExcel->getActiveSheet()->setCellValue('H' . ($i + 2), $list[$i]['grade']);// 
            $objPHPExcel->getActiveSheet()->setCellValue('I' . ($i + 2), $list[$i]['departmentsName']);// 
            $objPHPExcel->getActiveSheet()->setCellValue('J' . ($i + 2), $list[$i]['professionalName']);// 
            $objPHPExcel->getActiveSheet()->setCellValue('K' . ($i + 2), $list[$i]['clazz']);// 
            $objPHPExcel->getActiveSheet()->setCellValue('L' . ($i + 2), $list[$i]['createdDate']);// 
        }
        // 设置保存的Excel表格名称
        $filename = '院系体测_'.$xiangmu.'排名记录_' . date('YmdHis', time()) . ".xlsx";
        // 设置当前激活的sheet表格名称
        $objPHPExcel->getActiveSheet()->setTitle('运动记录');

        // 保存本地文件
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
        $objWriter->save(UPLOAD_TEMP_PATH . "/" . $filename);
        return download(UPLOAD_TEMP_PATH . "/" . $filename, $filename);

        // 文件地址
//        $filePath = get_image_url('/uploads'.str_replace(ATTACHMENT_PATH, "", UPLOAD_TEMP_PATH . "/" . $filename));
//        return $this->jsonReturn(message("操作成功", true, $filePath));
    }

    // 赛事数据成绩导出
    public function rankexportExcel2()
    {
        // 参数
        $param = request()->param();
        // 查询条件
        $map = [];


        if ($param) {
            $taskId = isset($param['taskId']) ? (int)$param['taskId'] : 0;
            if ($taskId) {
                $map[] = ['taskId', '=', $taskId];
                $map[] = ['type', '=', 3];
            }
            // 学年
            if (isset($param['gradeId']) && $param['gradeId']) {
                $map[] = ['gradeId', '=', $param['gradeId']];
            }

            // 院系
            if (isset($param['departmentsId']) && $param['departmentsId']) {
                $map[] = ['departmentsId', '=', $param['departmentsId']];
            }

            // 专业
            if (isset($param['professionalId']) && $param['professionalId']) {
                $map[] = ['professionalId', '=', $param['professionalId']];
            }

            // 班级
            if (isset($param['classId']) && $param['classId']) {
                $map[] = ['classId', '=', $param['classId']];
            }

            // 性别
            if (isset($param['gender']) && $param['gender']) {
                $map[] = ['gender', '=', $param['gender']];
            }

        }

        // 获取导出的数据源
        $list = $this->model->where($map)->order('pingscore desc')->group('memberId')->field('MAX(pingscore) as pingscore')->column("id");


        $listTmp = [];
        if (is_array($list)) {
            foreach ($list as $val) {
                $info = $this->model->getInfo($val);
                $listTmp[] = $info;
            }
        }

        $list = $listTmp;
        // 实例化PHPExcel类
        $objPHPExcel = new \PHPExcel();
        // 激活当前的sheet表
        $objPHPExcel->setActiveSheetIndex(0);
        //5.设置表格头（即excel表格的第一行）
        $objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', '姓名')
            ->setCellValue('B1', '学籍号')
            ->setCellValue('C1', '运动成绩')
            ->setCellValue('D1', '运动耗时')
            ->setCellValue('E1', '评分')
            ->setCellValue('F1', '性别')
            ->setCellValue('G1', '年级')
            ->setCellValue('H1', '院系名称')
            ->setCellValue('I1', '专业')
            ->setCellValue('J1', '专业班级')
            ->setCellValue('K1', '运动时间');
        // 设置表格头水平居中
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('A1')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('B1')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('C1')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('D1')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('E1')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('F1')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('G1')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('H1')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('I1')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('J1')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('K1')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        //设置列水平居中
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('A')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('B')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('C')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('D')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('E')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('F')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('G')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('H')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('I')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('J')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('K')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        // 设置单元格宽度
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('A')->setWidth(10);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('B')->setWidth(10);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('C')->setWidth(10);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('D')->setWidth(10);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('E')->setWidth(10);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('F')->setWidth(10);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('G')->setWidth(10);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('H')->setWidth(10);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('I')->setWidth(10);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('J')->setWidth(10);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('K')->setWidth(10);
        $xiangmu = '';
        // 循环刚取出来的数组，将数据逐一添加到excel表格。
        for ($i = 0; $i < count($list); $i++) {
            $xiangmu = $list[$i]['projectName'];
            $objPHPExcel->getActiveSheet()->setCellValue('A' . ($i + 2), $list[$i]['testerName']);// 
            $objPHPExcel->getActiveSheet()->setCellValue('B' . ($i + 2), $list[$i]['testerNumber']);// 
            $objPHPExcel->getActiveSheet()->setCellValue('C' . ($i + 2), $list[$i]['score']);// 
            $objPHPExcel->getActiveSheet()->setCellValue('D' . ($i + 2), $list[$i]['duration']);// 
            $objPHPExcel->getActiveSheet()->setCellValue('E' . ($i + 2), $list[$i]['pingscore']);// 
            $objPHPExcel->getActiveSheet()->setCellValue('F' . ($i + 2), $list[$i]['gender']==1 ? "男" : "女");// 
            $objPHPExcel->getActiveSheet()->setCellValue('G' . ($i + 2), $list[$i]['grade']);// 
            $objPHPExcel->getActiveSheet()->setCellValue('H' . ($i + 2), $list[$i]['school']);// 
            $objPHPExcel->getActiveSheet()->setCellValue('I' . ($i + 2), $list[$i]['professionalName']);// 
            $objPHPExcel->getActiveSheet()->setCellValue('J' . ($i + 2), $list[$i]['clazz']);// 
            $objPHPExcel->getActiveSheet()->setCellValue('K' . ($i + 2), $list[$i]['createdDate']);// 
        }
        // 设置保存的Excel表格名称
        $filename = '体育赛事_'.$xiangmu.'排名记录_' . date('YmdHis', time()) . ".xlsx";
        // 设置当前激活的sheet表格名称
        $objPHPExcel->getActiveSheet()->setTitle('运动记录');

        // 保存本地文件
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
        $objWriter->save(UPLOAD_TEMP_PATH . "/" . $filename);
        return download(UPLOAD_TEMP_PATH . "/" . $filename, $filename);
        // 文件地址
//        $filePath = get_image_url('/uploads'.str_replace(ATTACHMENT_PATH, "", UPLOAD_TEMP_PATH . "/" . $filename));
//        return $this->jsonReturn(message("操作成功", true, $filePath));
    }
}